#!/usr/bin/env python  
# encoding: utf-8


""" 
@version: v1.0 
@author: Shylock 
@license: Apache Licence  
@contact: JYFelt@163.com 
@site: http://blog.csdn.net/weixin_38034182 
@software: PyCharm Community Edition 
@file: QInfoPage.py 
@time: 2017/12/28 9:46 
"""
from tkinter import *
from tkinter import messagebox as mb
import MYSQL_CL as ms
import MainPage as mp


# 求职者信息插入界面
class insertPage(object):
    def __init__(self, master=None):
        self.root = master  # 定义内部变量
        self.root.geometry('800x500')
        self.text_1 = StringVar()
        self.text_2 = StringVar()
        self.text_3 = StringVar()
        self.text_4 = StringVar()
        self.text_5 = StringVar()
        self.text_6 = StringVar()
        self.text_7 = StringVar()
        self.text_8 = StringVar()

        self.createPage()

    def createPage(self):
        self.page = Frame(self.root)
        self.page.pack()
        self.show = self.showPage()

    def showPage(self):
        Button(self.page, text='返回', command=self.jumpback).grid(row=10, column=2)
        # b2= Button(self.page, text='插入信息到公司', command=self.show).grid(row=10, column=1)
        b1 = Button(self.page, text='插入信息到求职者', command=self.show).grid(row=10, column=1)

    def show(self):
        Label(self.page, text='注意').grid(row=0, column=0)
        Label(self.page, text='*号为必填项目').grid(row=0, column=1)
        Label(self.page, text='*请输入求职者ID: ').grid(row=2, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_1).grid(row=2, column=1, stick=E)

        Label(self.page, text='请输入姓名: ').grid(row=3, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_2).grid(row=3, column=1, stick=E)

        Label(self.page, text='请输入工资要求: ').grid(row=4, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_3).grid(row=4, column=1, stick=E)

        Label(self.page, text='请输入要求的工作的ID: ').grid(row=5, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_4).grid(row=5, column=1, stick=E)

        Label(self.page, text='性别: ').grid(row=6, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_5).grid(row=6, column=1, stick=E)

        Label(self.page, text='年龄: ').grid(row=7, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_6).grid(row=7, column=1, stick=E)

        Label(self.page, text='就业标志（就业\未就业）: ').grid(row=8, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_7).grid(row=8, column=1, stick=E)

        Label(self.page, text='就业工作: ').grid(row=9, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_8).grid(row=9, column=1, stick=E)

        Button(self.page, text='插入', command=self.exec_).grid(row=10, column=0)

    def exec_(self):
        list = []
        list.append(self.text_1.get())
        list.append(self.text_2.get())
        list.append(self.text_3.get())
        list.append(self.text_4.get())
        list.append(self.text_5.get())
        list.append(self.text_6.get())
        list.append(self.text_7.get())
        list.append(self.text_8.get())
        # for i in range(len(list)):
        # print(list[i])
        __sql_1 = "SELECT ID FROM 求职者信息"
        t = ms.exec(__sql_1)

        __sql = "INSERT INTO 求职者信息(ID,姓名,工资要求,要求工作ID,性别,年龄,就业标志,就业工作) VALUES('%s','%s','%s','%s','%s','%s','%s','%s')" % (
            list[0], list[1], list[2], list[3], list[4], list[5], list[6], list[7])
        for i in range(len(t)):
            if self.text_1.get() not in (t[i]):
                ms.update(sql=__sql)
                Label(self.page, text='插入成功').grid(row=11, column=1)
            else:
                mb.showwarning('啊呀，出错啦~', message='插入失败,ID重复')

                # print(__sql)

    def jumpback(self):
        self.page.destroy()
        mp.MainPage(self.root)


# 查询界面 over
class searchPage(object):
    def __init__(self, master=None):
        self.root = master  # 定义内部变量
        self.root.geometry('800x500')
        self.createPage()

    def createPage(self):
        self.page = Frame(self.root)
        self.page.pack()
        self.show = self.showPage()

    def showPage(self):

        # Button(self.page, text='查询所有公司信息', command=self.gongsi).grid(row=0, column=0)
        Button(self.page, text='查询所有求职者信息', command=self.qiuzhizhe).grid(row=0, column=1)
        Button(self.page, text='返回', command=self.jumpback).grid(row=0, column=5)

    def qiuzhizhe(self):
        t = ms.exec(sql='SELECT * FROM 求职者信息')
        print(t)
        text = ('求职者ID', '姓名', '工资要求', '需求工作ID', '性别', '年龄', '就业标志', '就业工作')

        for i in range(len(text)):
            a = text[i]
            Label(self.page, text=a).grid(row=1, column=i)

        for i in range(len(t)):
            for j in range(len(t[i])):
                a = t[i][j]
                Label(self.page, text='%s' % str(a)).grid(row=i + 2, column=j)

    def jumpback(self):
        self.page.destroy()
        mp.MainPage(self.root)


# 修改、删除界面
class modifyPage(object):
    def __init__(self, master=None):
        self.root = master  # 定义内部变量
        self.root.geometry('800x500')
        self.text_1 = StringVar()
        self.createPage()

    def createPage(self):
        self.page = Frame(self.root)
        self.page.pack()
        self.show = self.showPage()

    def showPage(self):
        Button(self.page, text='返回', command=self.jumpback).grid(row=0)
        Button(self.page, text='修改信息', command=self.modifypage).grid(row=0, column=1)
        Button(self.page, text='删除信息', command=self.deletepage).grid(row=0, column=2)

    def deletepage(self):
        Label(self.page, text='*请输入要删除的ID: ').grid(row=2, stick=W, pady=10)
        Entry(self.page, textvariable=self.text_1).grid(row=2, column=1, stick=E)

        Button(self.page, text='删除', command=self.delete).grid(row=3, column=2)

    def delete(self):
        self.t = self.text_1.get()
        #  print(self.t)
        self.__sql_1 = "SELECT ID FROM 求职者信息"
        self.__sql = "delete from 求职者信息 where ID='%s'" % (self.t)


        for i in range(len(ms.exec(self.__sql_1))):
            if self.t in (ms.exec(self.__sql_1)[i]):
                Label(self.page, text='删除中').grid(row=3)

                ms.update(self.__sql)

                if self.t not in (ms.exec(self.__sql_1)):
                    mb.showwarning('成功！', message='删除成功')
                else:
                    mb.showwarning('啊呀，出错啦~', message='删除失败，请重试')

            else:
                mb.showwarning('啊呀，出错啦~', message='删除错误，ID不存在')

    def modifypage(self):
        pass

    def jumpback(self):
        self.page.destroy()
        mp.MainPage(self.root)
